﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAn.BUS;
using DoAn.DTO;

namespace DoAn
{
    public partial class DangKi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dtNgayHetHan.Value = DateTime.Now.Date;
                dtNgaySinh.MaxDate = DateTime.Now.Date;
            }
            rvNgaySinh.MaximumValue = DateTime.Now.Month.ToString()+"/"+DateTime.Now.Day.ToString()+"/"+(DateTime.Now.Year-16).ToString();
            rvNgaySinh.MinimumValue = DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString() + "/" + (DateTime.Now.Year - 100).ToString();
            rvNgayHetHan.MinimumValue = DateTime.Now.Date.ToString("MM/dd/yyyy");
        }

        protected void cvKiemTraThe_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (TaiKhoanBUS.ValidateLuhn(args.Value.ToString()))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }

        }

        protected void bntDangNhap_Click(object sender, EventArgs e)
        {
            TaiKhoanDTO tk = new TaiKhoanDTO();
            tk = LayGiaTri();
            TaiKhoanBUS.dangKiTK(tk);
            Response.Redirect("KichHoatTaiKhoan.aspx?id=" + tk.TenTaiKhoan + "&makh=" + tk.MaKichHoatTK);
        }
        //Lấy giá trị các control
        public TaiKhoanDTO LayGiaTri()
        {
            TaiKhoanDTO tk = new TaiKhoanDTO();
            tk.DiaChi = txtDiaChi.Text;
            tk.Email = txtEmail.Text;
            tk.HoTen = txtHoTen.Text;
            tk.LoaiTaiKhoan = false;
            tk.LoaiTheNganHang = cmbLoaiThe.SelectedItem.Text;
            tk.MaKichHoatTK = Ramdomchar();
            tk.MaThe =TaiKhoanBUS.getSHA1Hash(txtMaThe.Text);
            tk.MatKhau = TaiKhoanBUS.getMd5Hash(txtMatKhau.Text);
            tk.NgayHetHan = dtNgayHetHan.Date;
            tk.NgaySinh = dtNgaySinh.Date;
            tk.SoCMND = txtCMND.Text;
            tk.TenChuThe = txtChuThe.Text;
            tk.TenTaiKhoan = txtTenTaiKhoan.Text;
            tk.TinhTrangKichHoatTK = false;
            tk.TinhTrangThe = true;
            return tk;
        }

        //Tạo chuỗi ngẫu nhiên 15 kí tự (mã xác nhận)
        public string Ramdomchar()
        {
            string s = "abcdefghiklmnopqrstuvxyz01234567890";
            Random ran = new Random();
            string kq="";
            for (int i = 0; i < 15; i++)
            {
                int index = ran.Next(s.Length - 1);
                kq += (s[index].ToString());//lay ngau nhien ki tu
            }
            return kq;
        }

        protected void bntHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("TrangChu.aspx");
        }
    }
}